class Solution
{
public:
    bool circularArrayLoop(vector<int> &nums)
    {
        int n = nums.size();
        for (int i = 0; i < n; ++i)
        {
            int start = i;
            int cur = i;
            int step = 0;
            int startVal = nums[i];
            while (true)
            {
                if (nums[cur] * startVal < 0) // 符号不一致
                {
                    break;
                }
                ++step;
                cur = (cur + nums[cur] % n + n) % n; // 取模和+n都是为了防止负数
                if (cur == start)
                {
                    if (step > 1)
                    {
                        return true;
                    }
                    else
                    {
                        break;
                    }
                }
                if (step > n) // 走了n步以上，则必然出现了循环，只需判断最后一步的步数是否是单步循环
                {
                    if (abs(nums[cur]) % n != 0)
                    {
                        return true;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        return false;
    }
};